package com.ttmusic.activitys;

import java.util.Date;
import java.util.Random;

import com.ttmusic.dao.MusicListDao;
import com.ttmusic.entity.MusicList;
import com.ttmusic.utils.SharedPreferencesUtil;
import com.ttmusic.utils.SQLite.SQLiteUtil;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;

/**
 * APP启动界面
 * @author wuchangyong
 *
 */
public class SplashActivity extends Activity {
	
	private RelativeLayout splash_container;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);
		
		
		splash_container = (RelativeLayout)findViewById(R.id.splash_container);
		
//		SQLiteUtil.dropTable("TB_ARTIST_INFO");
//		SQLiteUtil.dropTable("TB_ALBUM_INFO");
//		SQLiteUtil.dropTable("TB_MUSIC_INFO");
//		SQLiteUtil.dropTable("TB_MUSIC_LIST");
//		SQLiteUtil.dropTable("TB_MUSICLIST_MUSICINFO");
		
		//创建艺术家、专辑、歌曲表、歌单表、歌单与歌曲的中间表
		SQLiteUtil.createTableTB_ARTIST_INFO(true);
		SQLiteUtil.createTableTB_ALBUM_INFO(true);
		SQLiteUtil.createTableTB_MUSIC_INFO(true);
		SQLiteUtil.createTableTB_MUSIC_LIST(true);
		
		//SQLiteUtil.deleteAllData("TB_MUSIC_LIST");
		//SQLiteUtil.deleteAllData("TB_MUSICLIST_MUSICINFO");
		
		
		//默认插入一条歌单数据 “我的最爱”
		MusicListDao mld = MusicListDao.getInstance();
		MusicList ml = mld.loadByName("我的最爱");
		if(ml == null) 
			mld.save(new MusicList("我的最爱", 0, "icon_heart_red.png", new Date().getTime()));
		
		//获取APP被运行的次数
		final int runCount = SharedPreferencesUtil.getHasRunCount(this);
		
		int resID = R.drawable.img_splash0;
		switch (new Random().nextInt(10)) {
			case 0 : resID = R.drawable.img_splash0;break;
			case 1 : resID = R.drawable.img_splash1;break;
			case 2 : resID = R.drawable.img_splash2;break;
			case 3 : resID = R.drawable.img_splash3;break;
			case 4 : resID = R.drawable.img_splash4;break;
			case 5 : resID = R.drawable.img_splash5;break;
			case 6 : resID = R.drawable.img_splash6;break;
			case 7 : resID = R.drawable.img_splash7;break;
			case 8 : resID = R.drawable.img_splash8;break;
			case 9 : resID = R.drawable.img_splash9;break;
			default: break;
		}
		//设置背景图片
		splash_container.setBackgroundDrawable(getResources().getDrawable(resID));
		
		//类似于js的setTimeout  延迟一段时间后执行一段程序
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				if(runCount == 0) {//第一次运行APP
					Intent intent = new Intent(SplashActivity.this, GuideActivity.class);  
					startActivity(intent); 
					overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
					finish();  
				}else {
					Intent intent = new Intent(SplashActivity.this, MainActivity.class);  
					startActivity(intent); 
					overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
					finish();  
				}
			}
		}, 2000);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.splash, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
